package com.appkubes.mall.restful.application.user.bankcard.controller;

import com.appkubes.base.core.entity.vo.UserKeyVO;
import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.exception.BusinessException;
import com.appkubes.mall.core.entity.MallUserBankCards;
import com.appkubes.mall.restful.application.user.bankcard.entity.vo.BankCardAddVO;
import com.appkubes.mall.restful.application.user.bankcard.entity.vo.BankCardDelVO;
import com.appkubes.mall.restful.application.user.bankcard.service.BankCardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/1/13 21:31
 * @Description:
 */
@Slf4j
@Api(tags = "银行卡管理")
@RestController
@RequestMapping("/user/bankcard")
public class BankCardController {
    @Resource
    BankCardService bankCardService;

    @ApiOperation("获取银行卡列表")
    @GetMapping("/get")
    public Response<List<MallUserBankCards>> get(@ParamValid UserKeyVO userKeyVO) {
        //查询账户下的所有银行卡
        MallUserBankCards query = new MallUserBankCards();
        query.setUserKey(userKeyVO.getUserKey());
        List<MallUserBankCards> bankCards = bankCardService.selectListByEntity(query);
        return Response.success(bankCards);
    }

    @ApiOperation("添加银行卡")
    @PostMapping("/add")
    public Response<MallUserBankCards> add(@RequestBody @ParamValid BankCardAddVO bankCardAddVO) {
        try {
            MallUserBankCards query = new MallUserBankCards();
            query.setUserKey(bankCardAddVO.getUserKey());
            query.setBankCardNumber(bankCardAddVO.getBankCardNumber());
            List<MallUserBankCards> bankCards = bankCardService.selectListByEntity(query);
            if (bankCards.size() > 0l) {
                throw new BusinessException("银行卡已经存在");
            }
            MallUserBankCards addBankCard = new MallUserBankCards();
            BeanUtils.copyProperties(bankCardAddVO, addBankCard);
            addBankCard.setAppKey(null);
            if (bankCardService.save(addBankCard)) {
                return Response.success(addBankCard);
            }
            return Response.success("添加银行卡失败");
        } catch (BusinessException be) {
            return Response.error(be.getMessage());
        }

    }

    @ApiOperation("删除银行卡")
    @PostMapping("/del")
    public Response del(@ParamValid BankCardDelVO delVO) {
        Boolean flag = bankCardService.removeById(delVO.getUserBankCardKey());
        return Response.success(flag);
    }
}
